#ifndef _USART_APP_H_
#define _USART_APP_H_

#include "main.h"
#include "tx_api.h"

#pragma pack(1)

struct usart_tx_info {
    unsigned int   len;
    unsigned char *data;
};

#define USART_RX_BUF_LEN (512)

struct usart_rx_buf_info {
    unsigned int  head;
    unsigned int  tail;
    unsigned char data[USART_RX_BUF_LEN];
};

#define CMD_FRAME_BUF_LEN (512)

struct usart_cmd_info {
    unsigned int   cursor;
    unsigned short frame_len;
    unsigned char  frame_buf[CMD_FRAME_BUF_LEN];
};

typedef enum {
    _PROTOCOL_NONE = 0,     // 未知
    _PROTOCOL_DRIVER,       // 驱动源
    _PROTOCOL_SEED_SRC,     // 种子源
    _PROTOCOL_SEED_SRC_ZK,  // 展宽

    _PROTOCOL_DELAY_LINE,  // 延迟线
} _Protocol_Type_t;

typedef enum {
    _SEED_ZK_IDEL = 0,
    _SEED_ZK_ECHO_ON,
    _SEED_ZK_ECHO_OFF,
    _SEED_ZK_MOD_ON,
    _SEED_ZK_MOD_OFF,
    _SEED_ZK_VER_FULL,
    _SEED_ZK_MOD,
    _SEED_ZK_MT,
    _SEED_ZK_AST,
} _SeedZKCMD_t;

typedef enum {
    _SEED_IDEL          = 0,
    _SEED_OPEN_CLOSE    = 0xC1,
    _SEED_SET_POWER     = 0xC3,
    _SEED_REALTIME_INFO = 0xD3,
} _SeedCMD_t;

typedef enum {
    _DELAYLINE_IDEL            = (uint8_t)0,
    _DELAYLINE_SET_SPEED       = (uint8_t)0x02,  // 设置各通道速度系数
    _DELAYLINE_SET_RANGE       = (uint8_t)0x03,  // 设置各通道最大量程
    _DELAYLINE_READ_SPEED      = (uint8_t)0x0f,  // 读取各通道速度系数
    _DELAYLINE_READ_RANGE      = (uint8_t)0x10,  // 读取各通道最大量程
    _DELAYLINE_SET_DELAY       = (uint8_t)0x04,  // 设置各通道延迟值
    _DELAYLINE_SET_DELAY_ADD   = (uint8_t)0x05,  // 设置各通道延迟增加值
    _DELAYLINE_SET_DELAY_DEC   = (uint8_t)0x06,  // 设置各通道延迟减小值
    _DELAYLINE_SET_ZERO        = (uint8_t)0x07,  // 设置各通道归零
    _DELAYLINE_READ_DELAY      = (uint8_t)0x0C,  // 读取当前延迟值
    _DELAYLINE_READ_DELAYSET   = (uint8_t)0x0D,  // 读取各通道的延迟设定值
    _DELAYLINE_GET_STATE       = (uint8_t)0x0E,  // 查询各通道的运行状态
    _DELAYLINE_SET_LOOP0       = (uint8_t)0x08,  // 设置巡回起始值
    _DELAYLINE_SET_LOOP1       = (uint8_t)0x09,  // 设置巡回终止值
    _DELAYLINE_SET_LOOP2       = (uint8_t)0x0A,  // 设置巡回端点停留时间（按 ms 发送）
    _DELAYLINE_SET_UNLOOP      = (uint8_t)0x0B,  // 停止巡回模式
    _DELAYLINE_EMERGENCY       = (uint8_t)0x1A,  // 急停
    _DELAYLINE_NORMAL          = (uint8_t)0x1B,  // 急停恢复
    _DELAYLINE_RESET           = (uint8_t)0x1C,  // 重置/升级
    _DELAYLINE_SET_OFFSET      = (uint8_t)0x1D,  // 设置偏移系数
    _DELAYLINE_SET_VER         = (uint8_t)0x1E,  // 查询版本号
    _DELAYLINE_MOTOR_CTRL      = (uint8_t)0x20,  // 电机控制开关
    _DELAYLINE_SET_SN          = (uint8_t)0x2A,  // 设置 SN
    _DELAYLINE_READ_SN         = (uint8_t)0x2B,  // 读取 SN 号
    _DELAYLINE_GET_MOTOR_FAULT = (uint8_t)0x3C,  // 电机阻力故障返回
} _DelyaLineCMD_t;

#define UART_CMD_MAX (200)

typedef struct {
    _Protocol_Type_t protocolType;                       // 协议类型
    volatile uint8_t Data_Package_NUM;                   // 接收数据标识位，0代表无数据，1代表有数据
    uint8_t          Uart_Receive_Buffer[UART_CMD_MAX];  // 串口接收数据缓存区
} USART_Receive_FIFO_Typedef;

typedef struct {
    FunctionalState             Response_Wait;            // 等待标识位，ENABLE=等待反馈 DISABLE=不等待反馈
    uint32_t                    Origin_Timestamp;         // 时间戳，记录命令发送时间
    USART_Receive_FIFO_Typedef *q;                        // 指向反馈接收区
    uint8_t                     Send_Data[UART_CMD_MAX];  // 重发命令存储区
    uint8_t                     Send_Data_Num;            // 重发数据长度，不含校验字节与帧尾
    uint8_t                     Re_Send_Num;              // 重发次数，记录重发次数
    _SeedZKCMD_t                seed;
} Response_Wait_Typedef;

#pragma pack()

extern TX_MUTEX mutex_usart_dev;

extern TX_THREAD power_parm_query_thread_handle;
extern TX_THREAD usart_rx_thread_handle;

extern unsigned char power_parm_query_thread_stack[1024];
extern unsigned char usart_rx_thread_stack[1024];

extern Response_Wait_Typedef Response_Wait_PARA_Set;       // 参数设置命令反馈等待结构体
extern Response_Wait_Typedef Response_Wait_Open_Control;   // 出光控制命令反馈等待结构体
extern Response_Wait_Typedef Response_Wait_Close_Control;  // 出光控制命令反馈等待结构体
extern Response_Wait_Typedef Response_Wait_Reset;          // 复位命令反馈等待结构体
extern Response_Wait_Typedef Response_Wait_Heat_Ready;     // 热待机准备指令
extern Response_Wait_Typedef Response_Wait_Cold_Ready;     // 冷待机准备指令
extern Response_Wait_Typedef Response_parm_query;          // 冷待机准备指令
extern Response_Wait_Typedef Response_Wait_SeedSrc;
extern Response_Wait_Typedef Response_Wait_SeedSrcZK;
extern Response_Wait_Typedef Response_Wait_DelayLine;

extern unsigned char  power_comm_loss_cnt;
extern unsigned short brake_interrupt_cnt;
extern unsigned int   output_seq_err_cnt;
extern unsigned char  laser_close_loss_cnt;
extern unsigned char  laser_open_loss_cnt;
extern unsigned short power_query_cmd_cnt;
extern unsigned short laser_open_cmd_cnt;
extern unsigned short laser_close_cmd_cnt;
extern unsigned char  output_seq_err_cnt_mark;

extern uint8_t seedErrCnt;

void                   power_parm_query_thread(ULONG arg);
void                   usart_rx_thread(ULONG arg);
void                   init_usart_app_var(void);
void                   usart_send(unsigned char *data, unsigned int len);
void                   addDrvSrcCheckSum(unsigned char *p);
Function_State_Typedef seedSrcOpen(void);
Function_State_Typedef seedSrcZKCmd(_SeedZKCMD_t cmd);
Function_State_Typedef seedSrcOpenClose(uint8_t value);
Function_State_Typedef seedSrcCmd(_SeedCMD_t cmd, uint8_t len, uint8_t *buf);
Function_State_Typedef uartSendCmd(Response_Wait_Typedef *p);
Function_State_Typedef Uart_Send_Command(Response_Wait_Typedef *p);
Function_State_Typedef uartSeedCmd(Response_Wait_Typedef *p);
Function_State_Typedef uartDelayLineCmd(Response_Wait_Typedef *p);
Function_State_Typedef readParaQuery(void);

#endif
